<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Admin_Model
 * @subpackage DistributorPrice
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The domain for distributor price
 * 
 * @category   Zend
 * @package    Admin_Model
 * @subpackage DistributorPrice
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Admin_Model_DistributorProduct extends Zend_Db_Table_Abstract
{

    /**
     * The name of table
     * @var string 
     */
    protected $_name = 'distributor_product';

    /**
     * Save distributor price
     * @param array $row
     */
    public function save($row)
    {
        // check ID (insert or update)
        $id = isset($row['id']) ? $row['id'] : null;
        if ($id) {
            unset($row['id']);
            $this->update($row, array('id = ?' => $id));
        } else {
            $this->insert($row);
        }
    }

    /**
     * Find ID of a distributor product by product ID and distributor ID
     * @param string $name
     * @param integer $distributorId
     * @return integer
     */
    public function findIdByNameAndDistributorId($name, $distributorId)
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, 'id');
        $query->where('name = ?', $name);
        $query->where('distributor_id = ?', $distributorId);
        $result = $this->getAdapter()->fetchOne($query);
        return $result;
    }

    /**
     * Get distributor products by filter 
     * @return array 
     */
    public function getAllByFilter($name, $productId, $productMpn, $manufacturerName, $distributorName, $updatedFrom, $updatedTo, $priceFrom, $priceTo, $publicPriceFrom, $publicPriceTo, $sort, $direction, $page, $rowCount)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('dp' => $this->_name), array('dp.id', 'dp.name', 'dp.price as distributor_price', 'dp.updated', 'dp.product_id', 'dp.distributor_id'));
        $query->join(array('d' => 'distributor'), 'dp.distributor_id = d.id', 'd.name as distributor_name');
        $query->joinLeft(array('p' => 'product'), 'dp.product_id = p.id', array('p.mpn as product_mpn', 'p.price as shop_price'));
        $query->joinLeft(array('m' => 'manufacturer'), 'p.manufacturer_id = m.id', 'm.name as manufacturer_name');

        // Check filters
        if ($name) {
            $query->where('dp.name LIKE ?', '%' . $name . '%');
        }
        if ($productId) {
            $query->where('dp.product_id = ?', $productId);
        }
        if ($productMpn) {
            $query->where('p.mpn LIKE ?', $productMpn . '%');
        }
        if ($manufacturerName) {
            $query->where('m.name LIKE ?', $manufacturerName . '%');
        }
        if ($distributorName) {
            $query->where('d.name LIKE ?', $distributorName . '%');
        }
        if ($updatedFrom) {
            $query->where('dp.updated >= ?', $updatedFrom);
        }
        if ($updatedTo) {
            $query->where('dp.updated <= ?', $updatedTo);
        }
        if ($priceFrom) {
            $query->where('dp.price >= ?', $priceFrom);
        }
        if ($priceTo) {
            $query->where('dp.price <= ?', $priceTo);
        }
        if ($publicPriceFrom) {
            $query->where('p.price >= ?', $publicPriceFrom);
        }
        if ($publicPriceTo) {
            $query->where('p.price <= ?', $publicPriceTo);
        }
        if ($sort && $direction) {
            $query->order($sort . ' ' . $direction);
        }

        $query->limitPage($page, $rowCount);

        // Get invoices from DB
        $result = $this->getAdapter()->fetchAll($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

    /**
     * Get count of distributor products by filter 
     * @return integer 
     */
    public function getCountByFilter($name, $productId, $productMpn, $manufacturerName, $distributorName, $updatedFrom, $updatedTo, $priceFrom, $priceTo, $publicPriceFrom, $publicPriceTo)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('dp' => $this->_name), new Zend_Db_Expr('COUNT(*)'));
        $query->join(array('d' => 'distributor'), 'dp.distributor_id = d.id', '');
        $query->joinLeft(array('p' => 'product'), 'dp.product_id = p.id', '');
        $query->joinLeft(array('m' => 'manufacturer'), 'p.manufacturer_id = m.id', '');

        // Check filters
        if ($name) {
            $query->where('dp.name LIKE ?', $name . '%');
        }
        if ($productId) {
            $query->where('dp.product_id = ?', $productId);
        }
        if ($productMpn) {
            $query->where('p.mpn LIKE ?', $productMpn . '%');
        }
        if ($manufacturerName) {
            $query->where('m.name LIKE ?', $manufacturerName . '%');
        }
        if ($distributorName) {
            $query->where('d.name LIKE ?', $distributorName . '%');
        }
        if ($updatedFrom) {
            $query->where('dp.updated >= ?', $updatedFrom);
        }
        if ($updatedTo) {
            $query->where('dp.updated <= ?', $updatedTo);
        }
        if ($priceFrom) {
            $query->where('dp.price >= ?', $priceFrom);
        }
        if ($priceTo) {
            $query->where('dp.price <= ?', $priceTo);
        }
        if ($publicPriceFrom) {
            $query->where('p.price >= ?', $publicPriceFrom);
        }
        if ($publicPriceTo) {
            $query->where('p.price <= ?', $publicPriceTo);
        }

        // Get count of invoices from DB
        $result = $this->getAdapter()->fetchOne($query);
        return $result;
    }

    /**
     * Get min prices for all products
     * @return array 
     */
    public function getMinPrices()
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, array('product_id', new Zend_Db_Expr('min(price)')));
        $query->group('product_id');
        $result = $this->getAdapter()->fetchPairs($query);
        return $result;
    }

}
